//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace TestScenarios
{
    using System.Collections.Generic;
    using System.Configuration;
    using System.Security.Cryptography.X509Certificates;
    using Preflight;
    using Preflight.Model;
    using Preflight.Verifications;

    public class AcceptanceTestsVerificationFactory : IVerificationFactory
    {
        public ApplicationDescription ApplicationDescription
        {
            get
            {
                return new ApplicationDescription
                           {
                               Name = Constants.ApplicationName,
                               Category = Constants.ApplicationCategory,
                               Version = Constants.ApplicationVersion
                           };
            }
        }

        public IEnumerable<IVerification> GetVerifications()
        {
            yield return VerificationBuilder.Instance.BuildVerification(CreateCertificateVerification());
            yield return VerificationBuilder.Instance.BuildVerification(CreateServiceVerification());
            yield return VerificationBuilder.Instance.BuildVerification(CreateSqlServerVerification());
        }         

        private static SqlConnectionVerificationDescription CreateSqlServerVerification()
        {
            var description = new SqlConnectionVerificationDescription
                {
                    VerificationName = "Local SQL Server",
                    Category = "Critical",
                    GetConnectionString = () => GetConfigurationSetting("connectionString")
                };

            return description;
        }

        private static WebGetVerificationDescription CreateServiceVerification()
        {
            var description = new WebGetVerificationDescription
                {
                    VerificationName = "Data Service",
                    Category = "Services",
                    GetUrl = () => GetConfigurationSetting("serviceUri")
                };

            return description;
        }

        private static CertificateAccessVerificationDescription CreateCertificateVerification()
        {
            var description = new CertificateAccessVerificationDescription
                {
                    VerificationName = "X509 Certificate",
                    Category = "Resources",
                    GetStoreLocation = () => StoreLocation.LocalMachine,
                    GetStoreName = () => StoreName.My,
                    GetThumbprint = () => "cdba27d3bab4b8a11b6b487c271e68656acfaa2c"
                };

            return description;
        }

        private static string GetConfigurationSetting(string key)
        {
            return ConfigurationManager.AppSettings[key];
        }
    }
}